package top.wilbert.collegelink.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.wilbert.collegelink.user.entity.CompanyCertificationEntity;
import top.wilbert.collegelink.user.entity.CompanyEntity;
import top.wilbert.collegelink.user.service.CompanyCertificationService;
import top.wilbert.collegelink.user.service.CompanyService;
import top.wilbert.collegelink.common.utils.PageUtils;
import top.wilbert.collegelink.common.utils.R;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author Augus
 * @email 2531496343@qq.com
 * @date 2022-12-23 20:02:17
 */
@RestController
@RequestMapping("user/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;
    @Autowired
    private CompanyCertificationService companyCertificationService;

    /**
     * 返回注册成功的公司列表
     */
    @RequestMapping("/okList")
    public R okList() {
        List<CompanyEntity> companyEntities = companyService.okList();

        return R.ok().setData(companyEntities);
    }

    /**
     * 公司认证审核
     */
    @RequestMapping("/audit")
//    @RequiresPermissions("subject:company:save")
    public R audit(@RequestBody CompanyCertificationEntity company) {
        CompanyEntity companyName1 = companyService.getOne(new QueryWrapper<CompanyEntity>().eq("company_name", company.getCompanyName()));
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());

        if (companyName1 == null){
            CompanyEntity companyEntity = new CompanyEntity();
            companyEntity.setCompanyName(company.getCompanyName());
            companyEntity.setCertificationStatus(1);
            companyEntity.setCreateTime(date);
            companyService.save(companyEntity);

            CompanyEntity companyName = companyService.getOne(new QueryWrapper<CompanyEntity>().eq("company_name", company.getCompanyName()));
            company.setCompanyId(companyName.getCompanyId());
            company.setIsOk(1);
            company.setCreateTime(date);
            companyCertificationService.save(company);

            return R.ok();
        }
        else{
            return R.error("该公司已注册");
        }
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("subject:company:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = companyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{companyId}")
//    @RequiresPermissions("subject:company:info")
    public R info(@PathVariable("companyId") Long companyId) {
        CompanyEntity company = companyService.getById(companyId);

        return R.ok().put("company", company);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("subject:company:save")
    public R save(@RequestBody CompanyEntity company) {
        companyService.save(company);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("subject:company:update")
    public R update(@RequestBody CompanyEntity company) {
        companyService.updateById(company);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("subject:company:delete")
    public R delete(@RequestBody Long[] companyIds) {
        companyService.removeByIds(Arrays.asList(companyIds));

        return R.ok();
    }

}
